package leecode.stringAndArray.DoubleArray;

/*对角线遍历*/
public class FindDiagonalOrder {

    public int[] findDiagonalOrder(int[][] mat) {
        //行
        int row = mat.length;
        //列
        int col = mat[0].length;
        int count = row+col-1;

        int[] answer = new int[row*col];
        int answerIndex = 0;

        int a = 0;
        int b = 0;


        for (int i = 0; i < count; i++) {
            //从下往上
            if(i%2==0){
                while (a>=0 && b<col){
                    answer[answerIndex] = mat[a][b];
                    answerIndex++;
                    a--;
                    b++;
                }
                if(b<col){
                    a++;
                }else{
                    a = a+2;
                    b--;
                }
            }else{
                //从上往下
                while (b>=0 && a<row){
                    answer[answerIndex] = mat[a][b];
                    answerIndex++;
                    a++;
                    b--;
                }
                if (a<row){
                    b++;
                }else {
                    b=b+2;
                    a--;
                }
            }
        }
        
        
        return  answer;
    }
}
